package com.goodving.guli.fragment;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.goodving.guli.R;
import com.goodving.guli.activity.GuliHardwareSettingActivity;
import com.goodving.guli.activity.SearchWeightScaleActivity;
import com.goodving.guli.activity.SettingContainerActivity;
import com.goodving.guli.activity.UnBindActivity;
import com.goodving.guli.activity.ff;
import com.goodving.guli.activity.fh;
import com.goodving.guli.bt.C0668a;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.eventbus.EventBtOnOff;
import com.goodving.guli.bt.eventbus.EventScaleStatus;
import com.goodving.guli.bt.lua.SlidingUpPanelLayout;
import com.goodving.guli.bt.model.C3911i;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.profile.scale.WeightHwInfo;
import com.goodving.guli.bt.profile.scale.XMBLEManager;
import com.goodving.guli.bt.profile.scale.WeightProfile;
import com.goodving.guli.bt.ui.C1795p;
import com.goodving.guli.model.DeviceSource;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.utility.Constants;
import com.goodving.guli.view.C3247b;
import com.goodving.guli.view.C4220a;

import cn.com.smartdevices.bracelet.chart.LinePieChartView;
import de.greenrobot.event.EventBus;

/**
 * Created by john on 2015/12/4.
 */
public class SettingWeightFragment extends C1795p implements View.OnClickListener { // eE
    private static final String f10028c = "SettingWeightFragment";
    private static final int f10029d = 257;
    private static final int f10030o = 8193;
    WeightHwInfo f10031b;
    private TextView f10032e;
    private TextView f10033f;
    private View f10034g;
    private SlidingUpPanelLayout f10035h;
    private TextView f10036i;
    private LinePieChartView f10037j;
    private ImageView f10038k;
    private TextView f10039l;
    private SlidingUpPanelLayout.PanelSlideListener f10040m;
    private boolean f10041n;
    private boolean f10042p;

    class eF implements SlidingUpPanelLayout.PanelSlideListener {
        final /* synthetic */ SettingWeightFragment f10043a;

        eF(SettingWeightFragment eEVar) {
            this.f10043a = eEVar;
        }

        public int onGetPullDownDistance() {
            return 0;
        }

        public Boolean onGetPullDownDockEnable() {
            return Boolean.valueOf(false);
        }

        public float onGetThreshhold() {
            return 0.0f;
        }

        public float onGetThreshhold2() {
            return 0.0f;
        }

        public void onLastSlideOffset(View view, float f) {
        }

        public void onPanelAnchored(View view) {
            GuliLog.tracePrintInfo("SettingWeightFragment", "onPanelAnchored");
        }

        public void onPanelCollapsed(View view) {
            GuliLog.tracePrintInfo("SettingWeightFragment", "onPanelCollapsed");
        }

        public void onPanelExpanded(View view) {
            GuliLog.tracePrintInfo("SettingWeightFragment", "onPanelExpanded");
        }

        public void onPanelSlide(View view, Boolean bool, float f) {
            if (f <= 1.0f) {
                this.f10043a.m14706a(f);
            }
        }
    }

    public SettingWeightFragment() {
        this.f10033f = null;
        this.f10037j = null;
        this.f10040m = new eF(this);
        this.f10041n = false;
    }

    private void m14706a(float f) {
        if (f >= 0.0f && f <= 1.0f) {
        }
    }

    private void m14707a(View view) {
        this.f10035h = (SlidingUpPanelLayout) view.findViewById(R.id.fragment_setting_sliding_layout);
        this.f10035h.setOverlayed(true);
        this.f10035h.setEnableDragViewTouchEvents(true);
        this.f10035h.setPanelSlideListener(this.f10040m);
        this.f10035h.setSlidingEnabled(true);
        m14710d();
        this.f10032e = (TextView) view.findViewById(R.id.device_firmware_version_text);
        this.f10033f = (TextView) view.findViewById(R.id.mili_rebind_tips);
        this.f10036i = (TextView) view.findViewById(R.id.weight_settings_merge_state);
        m14711e();
        view.findViewById(R.id.device_unbind).setOnClickListener(this);
        view.findViewById(R.id.device_firmware_version).setOnClickListener(this);
        view.findViewById(R.id.device_unbind_in_mask).setOnClickListener(this);
        this.f10034g = view.findViewById(R.id.mask_view);
        this.f10037j = (LinePieChartView) view.findViewById(R.id.weight_battery_pie_chart);
        this.f10038k = (ImageView) view.findViewById(R.id.weight_device_battery_low_power);
        this.f10039l = (TextView) view.findViewById(R.id.weight_device_battery_tips);
        view.findViewById(R.id.weight_setting_babyweight).setOnClickListener(this);
        view.findViewById(R.id.weight_settings_about).setOnClickListener(this);
        view.findViewById(R.id.weight_settings_merge_ll).setOnClickListener(this);
        if (DeviceSource.hasBindWeight()) {
            this.f10033f.setText(R.string.unbind);
        } else {
            this.f10033f.setText(R.string.bind_weight_scale);
        }
    }

    public static SettingWeightFragment m14709c() {
        SettingWeightFragment eEVar = new SettingWeightFragment();
        eEVar.setArguments(new Bundle());
        return eEVar;
    }

    private void m14710d() {
        fh a = new ff(getActivity()).m14851a();
        if (a.m14878d()) {
            int e = a.m14879e();
            int dimensionPixelSize = getResources().getDimensionPixelSize(R.dimen.main_ui_panel_height);
            GuliLog.tracePrintInfo(f10028c, "SlidingPanel SystemBarH : " + e + ", OriginalPanelH : " + dimensionPixelSize);
            this.f10035h.setPanelHeight(dimensionPixelSize - e);
        }
    }

    private void m14711e() {
        if (Keeper.readPersonInfo().getMiliConfig().getWeightMergeResult()) {
            this.f10036i.setText(R.string.state_open);
        } else {
            this.f10036i.setText(R.string.state_close);
        }
    }

    private void m14712f() {
        this.f10038k.setVisibility(View.VISIBLE);
        this.f10039l.setVisibility(View.VISIBLE);
    }

    private void m14713g() {
        Intent intent = new Intent(getActivity(), UnBindActivity.class);
        intent.putExtra(Constants.VIA_RESULT_SUCCESS, Utils.f3495c);
        startActivityForResult(intent, f10029d);
    }

    private void m14714h() {
        GuliLog.tracePrintInfo(f10028c, "enableUI");
        this.f10034g.setVisibility(View.INVISIBLE);
        this.f10033f.setVisibility(View.VISIBLE);
    }

    private void m14715i() {
        m14716j();
    }

    private void m14716j() {
        WeightProfile i = XMBLEManager.m25164i();
        if (i == null || !i.m25529u()) {
            this.f10032e.setText(this.f10031b.fwVersion);
            return;
        }
        C3911i z = i.m25532z();
        if (z == null || z.f19720c == null) {
            this.f10032e.setText(this.f10031b.fwVersion);
        } else {
            this.f10032e.setText(z.f19720c);
        }
    }

    private void m14717k() {
//        Intent intent = new Intent(getActivity(), BabyWeightActivity.class);
//        intent.putExtra("CONNECTION_FAIL", this.f10041n);
//        startActivity(intent);
    }

    public boolean m14718a(boolean z) {
        BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        if (defaultAdapter == null || defaultAdapter.isEnabled()) {
            return true;
        }
        if (!z) {
            return false;
        }
        if (XMBLEManager.m25167l()) {
            return true;
        }
        startActivityForResult(new Intent("android.bluetooth.adapter.action.REQUEST_ENABLE"), f10030o);
        return false;
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        GuliLog.tracePrintInfo(f10028c, "onActivityResult: " + i + " result:" + i2);
        if (i == f10029d && i2 == -1 && getActivity() != null) {
            Activity activity = getActivity();
            if (activity == null) {
                return;
            }
            if (activity instanceof SettingContainerActivity) {
                ((SettingContainerActivity) activity).m14077b();
            } else if (activity instanceof GuliHardwareSettingActivity) {
                ((GuliHardwareSettingActivity) activity).m14067b();
            }
        } else if (i == f10030o) {
            switch (i2) {
                case -1 /*-1*/:
                    m14717k();
                case C4220a.f20886i /*0*/:
                    C3247b.m22930a(getActivity().getApplicationContext(), getString(R.string.failed_enable_bt), 1).show();
                default:
            }
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.weight_settings_merge_ll:
//                startActivity(new Intent(getActivity(), SettingMergeDuplicateActivity.class));
//            case R.id.weight_settings_about:
//                startActivity(new Intent(getActivity(), InstructionWeightActivity.class));
//                C0668a.m8126a(getActivity().getApplicationContext(), C0668a.fS);
            case R.id.device_unbind:
                if (DeviceSource.hasBindWeight()) {
                    m14713g();
                    return;
                }
                C0668a.m8126a(getActivity().getApplicationContext(), C0668a.fN);
                startActivity(new Intent(getActivity(), SearchWeightScaleActivity.class));
                getActivity().finish();
//            case R.id.device_firmware_version:
//                C3247b.m22930a(getActivity(), getActivity().getString(R.string.bracelet_info_mac_address, new Object[]{this.f10031b.name, this.f10031b.address}), 1).show();
            case R.id.device_unbind_in_mask:
                if (Utils.m8065e()) {
                    m14713g();
                    C0668a.m8126a(getActivity().getApplicationContext(), C0668a.fT);
                }
//            case R.id.weight_setting_babyweight:
//                GuliLog.tracePrintInfo(f10028c, "click " + this.f10042p);
//                if (this.f10042p) {
//                    m14718a(true);
//                } else {
//                    m14717k();
//                }
            default:
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.f10031b = Keeper.readWeightHwInfo();
        EventBus.getDefault().register(this);
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.setting_weight_fragment, viewGroup, false);
        m14707a(inflate);
        return inflate;
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public void onDestroyView() {
        super.onDestroyView();
        GuliLog.tracePrintInfo(f10028c, "onDestroyView");
    }

    public void onEvent(EventBtOnOff eventBtOnOff) {
        GuliLog.tracePrintInfo(f10028c, "onBtOnOff:" + eventBtOnOff.type);
        if (eventBtOnOff.type == 1) {
            this.f10042p = true;
        } else if (eventBtOnOff.type == 0) {
            this.f10042p = false;
        }
        GuliLog.tracePrintInfo(f10028c, "mBluetooth is " + this.f10042p);
    }

//    public void onEvent(EventWeightLowBattery eventWeightLowBattery) {
//        GuliLog.tracePrintInfo(f10028c, " EventWeightLowBattery() update drag view ");
//    }

    public void onEvent(EventScaleStatus eventScaleStatus) {
        GuliLog.tracePrintInfo(f10028c, "event scale status " + eventScaleStatus.status);
        if (eventScaleStatus.status == f10029d) {
            this.f10041n = false;
        } else {
            this.f10041n = true;
        }
    }

    public void onPause() {
        super.onPause();
    }

    public void onResume() {
        super.onResume();
        GuliLog.tracePrintInfo(f10028c, "onResume");
        m14715i();
        m14711e();
        m14714h();
    }
}
